﻿using System;

namespace DataScripter.Export.Code
{
	class CSharp
	{
		public static string ToDec(decimal? d)
		{
			return d.ToString().Replace(",", ".") + "M";
		}
		public static string ToEnum(Enum enumValue)
		{
			//    Frequency = RepaymentFrequency.Monthly,
			return enumValue.GetType().Name + "." + enumValue;
		}

		public static string ToDate(DateTime? nullabledate)
		{
			DateTime date = nullabledate.Value;
			return string.Format("new DateTime({0},{1},{2})", date.Year, date.Month, date.Day);
		}

		public static string SafeIdentifier(string name)
		{
			string[] parts = name.Split(' ');
			for (int i = 0; i < parts.Length - 1; i++)
			{
				parts[i] = parts[i].Replace("-", string.Empty);
			}
			return string.Join("", parts);
		}

		public static string SafeMethodName(string name)
		{
			return SafeIdentifier(name);
		}

		public static string ToCSharp(object property)
		{
			if (property == null)
				return "null";

			if (property.GetType().IsEnum)
			{
				return ToEnum((Enum) property);
			}

			switch(property.GetType().FullName)
			{
				case "System.Int32":
					return ToDec((int)property);
				case "System.DateTime":
					return ToDate((DateTime) property);
				case "System.Guid":
					return ToGuid((Guid)property);
				case "System.Decimal":
					return ToDec((decimal) property);
				case "System.Boolean":
					return ToBool((bool) property);
				default:
					return Quoted(property);
			}
		}

		public static string Quoted(object o)
		{
			return @"""" + o + @"""";
		}

		public static string ToGuid(Guid property)
		{
			return string.Format("new Guid({0})", Quoted(property));
		}

		public static string ToBool(bool? nullableBooleanValue)
		{
			bool booleanValue = nullableBooleanValue.Value;
			return booleanValue ? "true" : "false";
		}
	}
}
